//
// File: OyRenderer.cpp
//

#include "OyRenderer.h"


OyRenderer::OyRenderer(HINSTANCE hInst) 
{
	m_hInst   = hInst;
	m_pDevice = NULL;
	m_hDLL    = NULL;
}

OyRenderer::~OyRenderer(void) 
{ 
	Release(); 
}

HRESULT	OyRenderer::CreateDevice(const TCHAR *APIStr)
{
	TCHAR buffer[256];

	if( _tcscmp( APIStr, L"Direct3D9" ) == 0 ) 
	{
		m_hDLL = LoadLibrary( L"OyDirect3D9.dll" );
		if( !m_hDLL ) 
		{
			 MessageBox(NULL,
				L"Loading OyDirect3D9.dll from lib failed.",
				L"OyEngine - error", MB_OK | MB_ICONERROR);
			 return E_FAIL;
		}
	}
	else
	{
		_sntprintf_s( buffer, _countof(buffer), 256, L"API '%s' not yet supported.", APIStr);
		MessageBox(NULL, buffer, L"OyEngine - error", MB_OK | MB_ICONERROR);
		return E_FAIL;
	}

	CREATERENDERDEVICE g_fCreateRenderDevice = NULL;
	g_fCreateRenderDevice = (CREATERENDERDEVICE) GetProcAddress( m_hDLL, "CreateRenderDevice" );
	if( !g_fCreateRenderDevice ) 
		return E_FAIL;

	HRESULT hr = S_OK;
	hr = g_fCreateRenderDevice( m_hDLL, &m_pDevice );
	if( FAILED(hr) )
	{
		MessageBox(NULL,
			L"CreateRenderDevice() from lib failed.",
			L"OyEngine - error", MB_OK | MB_ICONERROR);
		m_pDevice = NULL;
		return E_FAIL;
	}

	return S_OK;
}

void OyRenderer::Release(void) 
{
	RELEASERENDERDEVICE g_fReleaseRenderDevice = 0;
	HRESULT hr = S_OK;
   
	if (m_hDLL) 
	{
		g_fReleaseRenderDevice = (RELEASERENDERDEVICE) GetProcAddress( m_hDLL, "ReleaseRenderDevice" );
	}

	if (m_pDevice) 
	{
		hr = g_fReleaseRenderDevice( &m_pDevice );
		if( FAILED(hr) )
		{
			m_pDevice = NULL;
		}
	}
}